/**
 * 
 */
package co.gov.medellin.sdep.sisdep.regulacion.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecSorteoException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecSorteoResultException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecSorteoResultRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaDecSorteoRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.descriptor.FeriaDecSorteoMessageDescriptor;
import co.gov.medellin.sdep.sisdep.regulacion.exception.descriptor.FeriaDecSorteoResultMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.FeriaDecSorteoResult;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IFeriaDecSorteoResultDAOT;

/**
 * @author Usuario
 *
 */
public class FeriaDecSorteoResultServiceImpl extends BaseService implements IFeriaDecSorteoResultService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IFeriaDecSorteoResultDAOT feriaDecSorteoResultDAO;

	/**
	 * 
	 */
	public FeriaDecSorteoResultServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(FeriaDecSorteoResult feriaDecSorteoResult) throws FeriaDecSorteoResultException {
		try{
			feriaDecSorteoResult.setUsuarioCrea(getPrincipalUser());
			feriaDecSorteoResult.setFechaCrea(new Date());
			getFeriaDecSorteoResultDAO().insert(feriaDecSorteoResult);
			this.updateGanadores(feriaDecSorteoResult);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new FeriaDecSorteoResultException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new FeriaDecSorteoResultException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(FeriaDecSorteoResult feriaDecSorteoResult) throws FeriaDecSorteoResultException {
		try{
			feriaDecSorteoResult.setUsuarioModifica(getPrincipalUser());			
			getFeriaDecSorteoResultDAO().update(feriaDecSorteoResult);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}
	
	public final void updateGanadores(FeriaDecSorteoResult feriaDecSorteoResult) throws FeriaDecSorteoResultException {
		try{
			feriaDecSorteoResult.setUsuarioModifica(getPrincipalUser());
			getFeriaDecSorteoResultDAO().updateGanadores(feriaDecSorteoResult);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(FeriaDecSorteoResult feriaDecSorteoResult) throws FeriaDecSorteoResultException {
		try {
			feriaDecSorteoResult.setUsuarioModifica(getPrincipalUser());
			feriaDecSorteoResult.setFechaModifica(new Date());
			getFeriaDecSorteoResultDAO().delete(feriaDecSorteoResult);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, FeriaDecSorteoResult feriaDecSorteoResult) throws FeriaDecSorteoResultException {
		try{
			//return getFeriaDecSorteoResultDAO().getAllFeriaDecSorteoResultPaginated(start, max, criteria);
			return getFeriaDecSorteoResultDAO().load();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
	
	/**
	 * @return 
	 * 
	 */
	public final List loadByAnio(int start,int max, FeriaDecSorteoResult feriaDecSorteoResult) throws FeriaDecSorteoResultException {
		try{
			//return getFeriaDecSorteoResultDAO().getAllFeriaDecSorteoResultPaginated(start, max, criteria);
			return getFeriaDecSorteoResultDAO().loadByAnio(feriaDecSorteoResult);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final FeriaDecSorteoResult get(long id) throws FeriaDecSorteoResultException {
		try{
			return getFeriaDecSorteoResultDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new FeriaDecSorteoResultRuntimeException(new FeriaDecSorteoResultMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List lstLugaresFeria() throws FeriaDecSorteoResultException {
		try{
			//return getFeriaDecSorteoDAO().getAllFeriaDecSorteoPaginated(start, max, criteria);
			return getFeriaDecSorteoResultDAO().lstLugaresFeria();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new FeriaDecSorteoRuntimeException(new FeriaDecSorteoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}	
	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IFeriaDecSorteoResultDAOT getFeriaDecSorteoResultDAO() {
		return feriaDecSorteoResultDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setFeriaDecSorteoResultDAO(IFeriaDecSorteoResultDAOT feriaDecSorteoResultDAO) {
		this.feriaDecSorteoResultDAO = feriaDecSorteoResultDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
